<<<<<<< HEAD:src/com/wwx/web/UserAction.java
package com.wwx.web;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.imageio.ImageIO;
import org.apache.struts2.ServletActionContext;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.opensymphony.xwork2.ActionSupport;
import com.wwx.entity.UserDO;
import com.wwx.service.UserService;
import com.wwx.service.impl.UserServiceImpl;
import com.wwx.util.PhoneSendCode;
import com.wwx.util.RandomCodeUtils;
/**
 * 用户登录,注册
 * @author XL
 *
 */
public class UserAction extends ActionSupport{
	
	private UserService userService=new UserServiceImpl();
	private UserDO user;
	private String code;
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public UserDO getUser() {
		return user;
	}
	public void setUser(UserDO user) {
		this.user = user;
	}
	
	public String execute() throws Exception {
		return super.execute();
	}
	
	/**
	 * 用于跳转到登录界面
	 */
	public String toLogin() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();  
		request.setAttribute("myRegister", "tab-2");
		return "tologin";
	}
	/**
	 * 用于跳转到注册界面
	 */
	public String toRegister() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();  
		request.setAttribute("myRegister", "tab-1");
		return "tologin";
	}
	
	/**
	 * 用于绘制验证码图片
	 */
	public void getVerificationCode() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();  
		HttpServletResponse resp = ServletActionContext.getResponse();
		RandomCodeUtils.sendCode(resp, request.getSession(), "codenumber");
	}
	
	/**
	 *  用于做登录
	 */
	public String doLogin() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();  
		HttpServletResponse resp = ServletActionContext.getResponse();
		if(request.getSession().getAttribute("codenumber").toString().toLowerCase().equals(code.toLowerCase())) {
			UserDO user=userService.checkUserByPasswordAndUsername(this.user.getUsername(), this.user.getPassword());
			if(user!=null) {
				request.getSession().setAttribute("myUser", user);
				return "cg";
			}else {
				resp.getWriter().print("<script>alert('账号或密码不对');history.go(-1)</script>");
				return null;
			}
		}else {
			resp.getWriter().print("<script>alert('验证码错误');history.go(-1)</script>");
			return null;
		}

	}
	/**
	 * 用于注册
	 */
	public String doRegister() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();  
		HttpServletResponse resp = ServletActionContext.getResponse();
		if(request.getSession().getAttribute("phone_code")
				.toString().toLowerCase().equals(code.toLowerCase())&&request.getSession()
				.getAttribute("phoneNumber").toString()
				.toLowerCase().equals(user.getUsername())) {
			boolean result=userService.addUser(user);
			if(result) {
				return "cg";
			}
			resp.getWriter().print("<script>alert('系统出现问题注册失败');history.go(-1)</script>");
			return null;
		}else {
			resp.getWriter().print("<script>alert('验证码错误');history.go(-1)</script>");
			return null;
		}
	}
	/**
	 * 用于给手手机发短信，获取验证码
	 */
	public void sendCode() throws ClientException {
		HttpSession session = ServletActionContext.getRequest().getSession();  
		String phone_code="";//验证码
		for(int i=0;i<6;i++) {
			phone_code+=(int)(Math.random()*10);
		}
		String phoneNumber=user.getUsername();//手机号
		PhoneSendCode sc=new PhoneSendCode();
		SendSmsResponse response = sc.sendSms(phoneNumber,phone_code);
		session.setAttribute("phone_code", phone_code);
		session.setAttribute("phoneNumber", phoneNumber);
	}

}
=======
package com.wwx.web;



import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.imageio.ImageIO;
import org.apache.struts2.ServletActionContext;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.opensymphony.xwork2.ActionSupport;
import com.wwx.entity.UserDO;
import com.wwx.service.UserService;
import com.wwx.service.impl.UserServiceImpl;
import com.wwx.util.SendCode;

public class UserWeb extends ActionSupport{
	
	private UserService userService=new UserServiceImpl();
	
	private UserDO user;
	
	private String code;
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public UserDO getUser() {
		return user;
	}
	public void setUser(UserDO user) {
		this.user = user;
	}
	
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	/**
	 * 用于跳转到登录界面
	 */
	public String toLogin() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();  
		request.setAttribute("myRegister", "tab-2");
		return "tologin";
	}
	/**
	 * 用于跳转到注册界面
	 */
	public String toRegister() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();  
		request.setAttribute("myRegister", "tab-1");
		return "tologin";
	}
	
	/**
	 * 用于绘制验证码图片
	 */
	public void getVerificationCode() throws Exception {
		int WIDTH=140; 
		int HEIGHT=30;
		String[] str = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R",
				"S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0"};
		Random r = new Random();
		HttpServletResponse resp=ServletActionContext.getResponse();
		ServletOutputStream sos = resp.getOutputStream();
		StringBuilder sb = new StringBuilder();
		StringBuilder validatestr = new StringBuilder();
		for (int i = 0; i < 6; i++) {
			int index = r.nextInt(str.length);
			sb.append(str[index]+" ");
			validatestr.append(str[index]);
		}
		//在内存中新建一张图片
		BufferedImage bi = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
		//获得这张图片的画笔
		Graphics g = bi.getGraphics();
		//画外框
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, WIDTH, HEIGHT);
		//填充这个矩形
		g.setColor(Color.YELLOW);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		//写字
		g.setColor(Color.RED);
		g.setFont(new Font("宋体",Font.BOLD,20));
		g.drawString(sb.toString(), 10, 22);
		//画干扰的线
		for (int i = 1; i <= 2; i++) {
			g.drawLine(10, i*10, 120, i*5);
		}
		g.drawLine(70,0,70,30);
		//画干扰的点
		for (int i = 1; i <300; i++) {
			int x = r.nextInt(140);
			int y = r.nextInt(30);
			g.drawLine(x,y,x,y);
		}
		HttpServletRequest request = ServletActionContext.getRequest();  
		request.getSession().setAttribute("codenumber", validatestr);
		System.out.println(validatestr);
		//销毁画笔
		g.dispose();
		ImageIO.write(bi, "jpg", sos);
		sos.flush();
		sos.close();
	}
	
	/**
	 *  用于做登录
	 */
	public String doLogin() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();  
		HttpServletResponse resp = ServletActionContext.getResponse();
		if(request.getSession().getAttribute("codenumber").toString().toLowerCase().equals(code.toLowerCase())) {
			UserDO user=userService.checkUserByPasswordAndUsername(this.user.getUsername(), this.user.getPassword());
			if(user!=null) {
				request.getSession().setAttribute("myUser", user);
				return "cg";
			}else {
				resp.getWriter().print("<script>alert('账号或密码不对');history.go(-1)</script>");
				return null;
			}
		}else {
			resp.getWriter().print("<script>alert('验证码错误');history.go(-1)</script>");
			return null;
		}

	}
	/**
	 * 用于注册
	 */
	public String doRegister() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();  
		HttpServletResponse resp = ServletActionContext.getResponse();
		if(request.getSession().getAttribute("phone_code").toString().toLowerCase().equals(code.toLowerCase())&&request.getSession().getAttribute("phoneNumber").toString().toLowerCase().equals(user.getUsername())) {
			boolean result=userService.addUser(user);
			if(result) {
				return "cg";
			}
			resp.getWriter().print("<script>alert('系统出现问题注册失败');history.go(-1)</script>");
			return null;
		}else {
			resp.getWriter().print("<script>alert('验证码错误');history.go(-1)</script>");
			return null;
		}
	}
	/**
	 * 用于给手手机发短信，获取验证码
	 */
	public void sendCode() throws ClientException {
		HttpSession session = ServletActionContext.getRequest().getSession();  
		String phone_code="";//验证码
		for(int i=0;i<6;i++) {
			phone_code+=(int)(Math.random()*10);
		}
		String phoneNumber=user.getUsername();//手机号
		SendCode sc=new SendCode();
		SendSmsResponse response = sc.sendSms(phoneNumber,phone_code);
		session.setAttribute("phone_code", phone_code);
		session.setAttribute("phoneNumber", phoneNumber);
	}

}
>>>>>>> dev:src/com/wwx/web/UserWeb.java
